// TOWN SCRIPT
//    Town 4: Proving Grounds - Fourth Level

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

// flags:
// 4, 2 = door unlocked
// 4, 3 = door unlocked
// 4, 5 = chest untrapped
// 4, 27 = balanced pre-sets
// 4, 28 = presets all gone
// 4, 29 = generate new monsters

begintownscript;

variables;

short i, j, choice;

body;

beginstate INIT_STATE;
	turn_off_training(1);
	set_crime_tolerance(1);

	set_flag(4, 0, 0);

	if (get_flag(4, 1) > 0) {
		erase_char(6);
	}

	// check monsters...
	run_scenario_script(18);
	if (get_flag(4, 29) > 0) {
		// generate some more...
		set_flag(4, 29, 0);
		print_str_color("Generating monsters...", 3);
		j = get_ran(1, 14, 19);
		i = 0;
		while (i < j) {
			run_scenario_script(30);
			if (get_flag(100, 0) > 0) {
				run_scenario_script(26);
				place_monster(get_flag(100, 1), get_flag(100, 2), get_flag(100, 0), 0);
			}
			i = i + 1;
		}
		run_scenario_script(39);
	}
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
break;

beginstate 10;
	if (get_flag(4, 0) == 0) {
		set_flag(4, 0, 1);
		play_sound(-162);
		reset_dialog();
		add_dialog_str(0, "Suddenly, a loud penetrating sound of an alarm can be heard.", 0);
		add_dialog_str(1, "There is a sudden silence as the alarm stops, followed by the clanking and tromping of guardian monsters.", 0);
		add_dialog_str(2, "The party gets the idea that they are in big trouble!", 0);
		add_dialog_choice(0, "OK");
		choice = run_dialog(0);
	}
break;

beginstate 11;
	reset_dialog();
	add_dialog_str(0, "There is an elevator here.", 0);
	add_dialog_str(1, "Use?", 0);
	add_dialog_choice(0, "No");
	add_dialog_choice(1, "Yes");
	choice = run_dialog(0);
	if (choice == 2) {
		block_entry(1);
		i = 0;
		while (i == 0) {
			get_text_response("Which level (A-D), D exits");
			check_text_response_match("A");
			if (got_text_match() > 0) {
				move_to_new_town(1, 25, 28);
				end();
			}
			check_text_response_match("B");
			if (got_text_match() > 0) {
				move_to_new_town(2, 24, 26);
				end();
			}
			check_text_response_match("C");
			if (got_text_match() > 0) {
				move_to_new_town(3, 23, 26);
				end();
			}
			check_text_response_match("D");
			if (got_text_match() > 0) {
				block_entry(0);
				end();
			}
		}
	}
break;

beginstate 12;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading up to the previous level.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb up");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder back up...
		move_to_new_town(3, 5, 26);
	}
break;

beginstate 13;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading down into the maze.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb down");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder down...
		move_to_new_town(5, 4, 43);
	}
break;

beginstate 14;
	if (get_flag(4, 1) == 0) {
		set_flag(4, 1, 1);
		reset_dialog();
		add_dialog_str(0, "The old man welcomes you with open arms.", 0);
		add_dialog_str(1, "_Congratulations_ he says _You are the first to make it this far._", 0);
		add_dialog_str(2, "He pulls a blue ribbon out and bestows it on the party.", 0);
		add_dialog_str(3, "_You now have access to the private elevator.  Go now, and God speed in your quest._", 0);
		add_dialog_str(4, "_Now I must go, so goodbye._", 0);
		add_dialog_str(5, "With a clap of his hands, he disappears.", 0);
		add_dialog_str(6, "You also find yourself being teleported.", 0);
		choice = run_dialog(0);
		change_spec_item(1, 1);
		put_effect_on_char(6, 0, 8, 1);
		run_animation_sound(10);
		erase_char(6);
		teleport_party(24, 21, 0);
	}
break;

beginstate 15;
	reset_dialog();
	add_dialog_str(0, "There is an elevator here.", 0);
	add_dialog_str(1, "Use?", 0);
	add_dialog_choice(0, "No");
	add_dialog_choice(1, "Yes");
	choice = run_dialog(0);
	if (choice == 2) {
		block_entry(1);
		i = 0;
		while (i == 0) {
			get_text_response("Which level (A-F), A exits");
			check_text_response_match("A");
			if (got_text_match() > 0) {
				block_entry(0);
				// move_to_new_town(4, 24, 43);
				end();
			}
			check_text_response_match("B");
			if (got_text_match() > 0) {
				move_to_new_town(5, 23, 43);
				end();
			}
			check_text_response_match("C");
			if (got_text_match() > 0) {
				move_to_new_town(6, 24, 43);
				end();
			}
			check_text_response_match("D");
			if (got_text_match() > 0) {
				move_to_new_town(7, 24, 43);
				end();
			}
			check_text_response_match("E");
			if (got_text_match() > 0) {
				move_to_new_town(8, 24, 9);
				end();
			}
			check_text_response_match("F");
			if (got_text_match() > 0) {
				move_to_new_town(9, 24, 43);
				end();
			}
		}
	}
break;

beginstate 16;
	if (get_flag(4, 4) == 0) {
		set_flag(4, 4, 1);
		reset_dialog();
		add_dialog_str(0, "In this room, you note a large semi-circular desk.", 0);
		add_dialog_str(1, "Upon it are the remains of what might have been scrying glasses and amulets of summoning and other artifacts of control and knowledge.", 0);
		add_dialog_str(2, "Unfortunately, they all seem to have been destroyed beyond repair.", 0);
		add_dialog_choice(0, "OK");
		choice = run_dialog(1);
	}
break;
